package com.kitty.common.schedule.task;

import com.kitty.common.schedule.entity.SpringScheduledCron;
import com.kitty.common.utils.SpringUtils;
import com.kitty.common.core.SchedulerManager;
import com.kitty.game.equip.service.NewChargeActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ChongZhiRankTask implements ScheduledOfTask{
    @Autowired
    Dao dao;

    @Override
    public void execute() {
        //开启抽奖排行榜
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try{
            List<SpringScheduledCron> SpringScheduledCronList = dao.query(SpringScheduledCron.class, Cnd.where("cron_key", "=","com.kitty.common.schedule.task.ChongZhiRankTask"));
            if(SpringScheduledCronList.size()>0){
                SpringScheduledCron springScheduledCron = SpringScheduledCronList.get(0);
                String cron_end = springScheduledCron.getCron_end();
                String[] split = cron_end.split(":");
                Calendar calendarEnd = Calendar.getInstance();
                calendarEnd.setTime(new Date());
                calendarEnd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));
                calendarEnd.set(Calendar.MINUTE, Integer.parseInt(split[1]));
                Calendar now=Calendar.getInstance();
                long nowTime=now.getTimeInMillis();
                long endTime = calendarEnd.getTimeInMillis();
                long time = endTime-nowTime;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String nowSdf = sdf.format(new Date());
                SpringUtils.getBean(NewChargeActivityService.class).startChongzhiank(nowSdf+" "+springScheduledCron.getCron_strat(),nowSdf+" "+springScheduledCron.getCron_end());

                //发送消息
                SchedulerManager.getInstance().schedule(new Runnable() {
                    @Override
                    public void run() {
                        SpringUtils.getBean(NewChargeActivityService.class).endChongZhiRank();
                    }
                }, time);
            }
        }catch (Exception e){
            log.info("充值排行榜开启异常：",e);
        }

        stopWatch.stop();
        log.info("充值排行榜开始，用时{}毫秒", stopWatch.getTime());
    }

    public void rebootExecute() {
        //开启抽奖排行榜
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try{
            List<SpringScheduledCron> SpringScheduledCronList = dao.query(SpringScheduledCron.class, Cnd.where("cron_key", "=","com.kitty.common.schedule.task.ChongZhiRankTask"));
            if(SpringScheduledCronList.size()>0){
                SpringScheduledCron springScheduledCron = SpringScheduledCronList.get(0);
                String cron_end = springScheduledCron.getCron_end();
                String[] split = cron_end.split(":");
                Calendar calendarEnd = Calendar.getInstance();
                calendarEnd.setTime(new Date());
                calendarEnd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));
                calendarEnd.set(Calendar.MINUTE, Integer.parseInt(split[1]));
                Calendar now=Calendar.getInstance();
                long nowTime=now.getTimeInMillis();
                long endTime = calendarEnd.getTimeInMillis();
                long time = endTime-nowTime;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String nowSdf = sdf.format(new Date());
                SpringUtils.getBean(NewChargeActivityService.class).rebootChongzhiRank(nowSdf+" "+springScheduledCron.getCron_strat(),nowSdf+" "+springScheduledCron.getCron_end());
                SchedulerManager.getInstance().schedule(new Runnable() {
                    @Override
                    public void run() {
                        SpringUtils.getBean(NewChargeActivityService.class).endChongZhiRank();
                    }
                }, time);
            }
        }catch(Exception e){
            log.info("充值排行榜重启异常：",e);
        }
        stopWatch.stop();
        log.info("充值排行榜重启，用时{}毫秒", stopWatch.getTime());
    }

}
